package org.code.mallspringboot.controller;

import org.code.mallspringboot.vo.CommonResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/minio")
public class UploadController {

    @PostMapping("/upload")
    public String upload(@RequestParam(required = false) MultipartFile file) {
        String message = "";
        boolean flag = true;
        if (!file.isEmpty()) {
            String path = "F:\\Chapter20素菜\\mall-spring-boot\\src\\main\\java\\org\\code\\mallspringboot\\images";
            //原文件名
            String originalFilename = file.getOriginalFilename();
            System.out.println("原文件名: "+originalFilename);
            File fileName = new File(path);
            if (!fileName.exists()){
                fileName.mkdirs();
            }
            try {
                file.transferTo(new File(fileName,originalFilename));
            } catch (IOException e) {
                flag = false;
                message = ("文件格式不正确");
                e.printStackTrace();
            }
            message = path + originalFilename;
        } else {
            message = ("文件必填");
            flag = false;
        }
        //返回
        if (flag) {
            return message;
        } else {
            return message;
        }

    }
}
